﻿using System;
using Bricks;
using Bricks.Injecting;
using Bricks.Unity.Injecting;
using Microsoft.Practices.Unity;

namespace Motar.Web.UnityExtensions
{
    
    /// <summary>
    /// 
    /// </summary>
    public static class UnityContainerRegistryFacadeExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest(this IContainerRegistryFacade containerRegistryFacade, Type serviceType)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest(this IContainerRegistryFacade containerRegistryFacade, Type serviceType, string key)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, key, new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest<TService>(this IContainerRegistryFacade containerRegistryFacade)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService>(new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest<TService>(this IContainerRegistryFacade containerRegistryFacade, string key)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService>(key, new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <param name="implType"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest(this IContainerRegistryFacade containerRegistryFacade, Type serviceType, Type implType)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, implType, new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <param name="implType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest(this IContainerRegistryFacade containerRegistryFacade, Type serviceType, Type implType, string key)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, implType, key, new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest<TService, TImpl>(this IContainerRegistryFacade containerRegistryFacade) where TImpl : TService
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService, TImpl>(new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerRequest<TService, TImpl>(this IContainerRegistryFacade containerRegistryFacade, string key) where TImpl : TService
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService, TImpl>(key, new HttpRequestLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession(this IContainerRegistryFacade containerRegistryFacade, Type serviceType)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession(this IContainerRegistryFacade containerRegistryFacade, Type serviceType, string key)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, key, new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession<TService>(this IContainerRegistryFacade containerRegistryFacade)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService>(new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession<TService>(this IContainerRegistryFacade containerRegistryFacade, string key)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService>(key, new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <param name="implType"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession(this IContainerRegistryFacade containerRegistryFacade, Type serviceType, Type implType)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, implType, new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="serviceType"></param>
        /// <param name="implType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession(this IContainerRegistryFacade containerRegistryFacade, Type serviceType, Type implType, string key)
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType(serviceType, implType, key, new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession<TService, TImpl>(this IContainerRegistryFacade containerRegistryFacade) where TImpl : TService
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService, TImpl>(new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="containerRegistryFacade"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IContainerRegistryFacade RegisterPerSession<TService, TImpl>(this IContainerRegistryFacade containerRegistryFacade, string key) where TImpl : TService
        {
            containerRegistryFacade.As<UnityContainerRegistryFacade>().Execute(unityContainer => unityContainer.RegisterType<TService, TImpl>(key, new HttpSessionLifetimeManager()));

            return containerRegistryFacade;
        }
    }
}
